package com.cnzz.site1255174697.factory;

import android.content.Context;

import com.cnzz.site1255174697.ConstHost;
import com.cnzz.site1255174697.model.MallEntity;
import com.cnzz.site1255174697.model.MallObjectEntity;
import com.cnzz.site1255174697.model.MallTuiEntity;
import com.cqyanyu.yanyu.http.XCallback;
import com.cqyanyu.yanyu.http.XPage;
import com.cqyanyu.yanyu.http.XResultPage;
import com.google.gson.Gson;

import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * 商品工具类
 */
public class GoodsFactory {

    //获取淘宝API--搜索分类
    public static void getGoods(Context context, String url, String parameter, final int page, final XCallback callback) {
        RequestParams param = new RequestParams(ConstHost.GET_GOODS_URL);
        param.addBodyParameter("requestName", url);
        param.addBodyParameter("parameter", parameter);
        x.http().get(param, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MallEntity mall = gson.fromJson(result, MallEntity.class);
                XResultPage xResultPage = new XResultPage();
                XPage<MallEntity.DataBean.ResultsBean.AitaobaoItemBean> xPage = new XPage();
                xPage.data = mall.getData().getResults().getAitaobao_item();
                xPage.page = page;
                xPage.totalPages = (int) Math.rint(mall.getData().getTotal_results() / 100);
                xResultPage.setData(xPage);
                if (mall.getCode() == 0) {
                    callback.onSuccess(xResultPage);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 获取商品详情列表
     *
     * @param context
     * @param url
     * @param parameter
     * @param callback
     */
    public static void getGoodsInfo(Context context, String url, String parameter, final Callback.CommonCallback callback) {
        RequestParams param = new RequestParams(ConstHost.GET_GOODS_URL);
        param.addBodyParameter("requestName", url);
        param.addBodyParameter("parameter", parameter);
        x.http().get(param, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MallTuiEntity mall = gson.fromJson(result, MallTuiEntity.class);
                if (mall.getCode() == 0) {
                    callback.onSuccess(mall);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();
            }
        });
    }

    /**
     * 单一商品
     *
     * @param context
     * @param url
     * @param parameter
     * @param callback
     */
    public static void getGoodsOneInfo(Context context, String url, String parameter, final Callback.CommonCallback callback) {
        RequestParams param = new RequestParams(ConstHost.GET_GOODS_URL);
        param.addBodyParameter("requestName", url);
        param.addBodyParameter("parameter", parameter);
        x.http().get(param, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Gson gson = new Gson();
                MallObjectEntity mall = gson.fromJson(result, MallObjectEntity.class);
                if (mall.getCode() == 0) {
                    callback.onSuccess(mall);
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
            }

            @Override
            public void onCancelled(CancelledException cex) {
            }

            @Override
            public void onFinished() {
                callback.onFinished();

            }
        });
    }
}
